define(['AMap'],function (AMap) {
    'use strict';
    var editMap = function (domId, dataObj) {
        const that = this;
        this.editor = {};
        // 折线覆盖物
        this.path = [];
        // 地图中心点
        this.centerPostition = [];
        // 折线图编辑开关
        this.isEditPolygon = false;
        // 编辑坐标开关
        this.isEditMarker = false;
        this.newPath = [];
        this.markerSpot = [];
        this.markers = [];

        this.feature = [
            'bg',
            'road',
            'building'
        ]

        this.nearbyMarkerDom = '' +
            '<div class="map-market">' +
            '  <img>' +
            '</div>';

        this.infoWinDom = '' +
            '<div class="map-click-win">' +
            '  <span></span>' +
            '</div>';

        if (typeof dataObj === 'undefined') {
            this.centerPostition = [110.287499, 19.983892]
            this.path = [
                [110.285032, 19.984538],
                [110.289666, 19.984659],
                [110.289752, 19.982118],
                [110.285311, 19.982299]
            ]
            this.markerSpot = this.centerPostition
            this.newPath = [
                [110.285032, 19.984538],
                [110.289666, 19.984659],
                [110.289752, 19.982118],
                [110.285311, 19.982299]
            ]
        } else {
            this.centerPostition = dataObj.place
            this.path = dataObj.path
            this.markerSpot = this.centerPostition
            this.path.forEach(function (t) {
                that.newPath.push([t.lng, t.lat])
            });
        }

        this.mapInfoWin = new AMap.InfoWindow({
            isCustom: true,
            offset: new AMap.Pixel(0, -40)
        });

        // 地图初始化
        this.map = new AMap.Map(domId, {
            resizeEnable: true,
            zoom: 15,
            center: this.centerPostition
        });

        // 服务点标记
        this.placeSearch = new AMap.PlaceSearch({
            //构造地点查询类
            showCover: true,
            city: "haikou" //城市
        });

        this.marker = new AMap.Marker({
            map: this.map,
            position: this.map.getCenter(),
            cursor: 'move',
            raiseOnDrag: true,
            icon: new AMap.Icon({
                image: "../../images/add-house-coordinate.png"
            })
        });
        //鼠标移动坐标点停止事件
        this.marker.on('dragend', function () {
            that.centerPostition = that.marker.getPosition()
            that.map.setCenter(that.marker.getPosition())
            that.markerSpot = [that.map.getCenter().lng, that.map.getCenter().lat]
        });

        this.editor._polygon = (function () {
            return new AMap.Polygon({
                map: that.map,
                path: that.path,
                strokeColor: "#0000ff",
                strokeOpacity: 1,
                strokeWeight: 3,
                fillColor: "#f5deb3",
                fillOpacity: 0.35
            });
        })();
        this.map.setFeatures(this.feature)
        this.marker.setMap(this.map);
        this.map.plugin(["AMap.PolyEditor"], function () {
            that.editor._polygonEditor = new AMap.PolyEditor(that.map, that.editor._polygon);
        })
    }

    editMap.prototype.search = function (searchString) {
        var that = this
        //关键字查询
        this.placeSearch.searchNearBy(searchString, this.centerPostition, 1000, function (status, result) {
            that.mapInfoWin.close();
            var markerUrl = '';
            if (that.markers.length > 0) {
                that.map.remove(that.markers)
                that.markers = []
            }
            switch (searchString) {
                case "学校": {
                    markerUrl = '../../images/map-school.jpg'
                    break
                }
                case "医院": {
                    markerUrl = '../../images/map-hospital.jpg'
                    break
                }
                case "超市": {
                    markerUrl = '../../images/map-supermarket.jpg'
                    break
                }
                case "商场": {
                    markerUrl = '../../images/map-market.jpg'
                    break
                }
                case "公交车站": {
                    markerUrl = '../../images/map-bus.jpg'
                    break
                }
            }
            if (status === 'complete' && result.info === 'OK') {
                for (var i = 0; i < result.poiList.pois.length; i++) {
                    var content = $(that.nearbyMarkerDom);
                    content
                        .find('img')
                        .attr('src', markerUrl)
                        .parent();

                    var mk = new AMap.Marker({
                        position: [result.poiList.pois[i].location.lng, result.poiList.pois[i].location.lat],
                        content: content[0]
                    });

                    var beforeMkDom;
                    (function (i) {
                        mk.on('click', function (e) {
                            var content = $(that.infoWinDom)
                            var target = e.target;
                            content
                                .find('span')
                                .html(result.poiList.pois[i].name)
                                .parent();

                            // 清除之前dom节点的active状态
                            typeof beforeMkDom !== 'undefined' ? beforeMkDom.removeClass('active') : '';

                            var mkDom = $(target.getContent());
                            mkDom.addClass('active');
                            beforeMkDom = mkDom;
                            that.mapInfoWin.setContent(content[0]);
                            that.mapInfoWin.open(that.map, target.getPosition());
                        }, that)
                    }(i))

                    that.markers.push(mk)
                }
                that.map.add(that.markers)
            }
        });
    };


    editMap.prototype.editMarker = function () {
        if (!this.isEditMarker) {
            this.isEditMarker = true
            this.marker.setDraggable(this.isEditMarker)
        } else {
            this.isEditMarker = false
            this.marker.setDraggable(this.isEditMarker)
        }
    }

    editMap.prototype.editPolygon = function () {
        var that = this
        if (!this.isEditPolygon) {
            this.editor._polygonEditor.open();
            this.isEditPolygon = true
        } else {
            this.editor._polygonEditor.close();
            this.isEditPolygon = false;
            this.newPath = []
            this.editor._polygon.getPath().forEach(function (t) {
                that.newPath.push([t.lng, t.lat])
            });
        }
    };
    return editMap
})
